package com.hundsun.fcp.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.hundsun.fcp.R;
import com.hundsun.fcp.base.BaseActivity;
import com.hundsun.fcp.common.Redirect;
import com.hundsun.fcp.common.TradeRequest;
import com.hundsun.fcp.common.utils.MD5;
import com.hundsun.fcp.common.utils.StringUtils;
import com.hundsun.fcp.manager.FcpUserManager;
import com.hundsun.fcp.openapi.trade.bean.RequestLoginBean;
import com.hundsun.fcp.widget.EditTextWithClear;

public class MineModifyGesturePwdActivity extends BaseActivity implements
		OnClickListener {
	// 当前手机号
	private TextView curMobile;
	// 当前登入密码
	private EditTextWithClear loginPwd;
	// 忘记登陆密码
	private TextView forgetPwd;
	// 修改手势密码
	private Button confirmModify;

	private String strCurPwd;
	
	private String mMobile ;

	protected int getContentLayout() {
		return R.layout.fcp_mine_modify_gesturepwd;
	}

	@Override
	protected void initHeader() {
		super.initHeader();
		mHeaderMenu.setTitle(R.string.mine_modify_gesture_pwd);
	}

	@Override
	public void initView() {
		super.initView();
		curMobile = (TextView) findViewById(R.id.tv_mobile);
		loginPwd = (EditTextWithClear) findViewById(R.id.et_curpwd);
		forgetPwd = (TextView) findViewById(R.id.tv_forget_pwd);
		confirmModify = (Button) findViewById(R.id.btn_submit);

		forgetPwd.setOnClickListener(this);
		confirmModify.setOnClickListener(this);
		mMobile = FcpUserManager.getInstance().getUserMobile() ;
		curMobile.setText(StringUtils.showMobileCiphertext(mMobile)) ;

		loginPwd.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			public void afterTextChanged(Editable s) {
				vfySignBtn();
			}
		});
	}

	@Override
	public void onClick(View v) {
		if (v == forgetPwd) {
			Redirect.startResetLoginPwd(this);
		} else if (v == confirmModify) {
			//已经验证非空 可直接使用
			strCurPwd = loginPwd.getText().toString().trim();
			RequestLoginBean loginBean = new RequestLoginBean() ;
			loginBean.mobile = mMobile ;
			loginBean.password = MD5.getMD5Str(strCurPwd) ;
			TradeRequest.doLogin(this, loginBean, false, true) ;
		}
	}
	
	@Override
	public void afterLogin() {
		super.afterLogin();
		setResult(RESULT_OK) ;
		this.finish() ;
	}

	/**
	 * 根据是否输入当前密码来控制提交按钮
	 */
	private void vfySignBtn() {
		strCurPwd = loginPwd.getText().toString().trim();
		if (StringUtils.isEmpty(strCurPwd)) {
			confirmModify.setEnabled(false);
			confirmModify.setBackgroundResource(R.drawable.btn_grey_unable);
		} else {
			confirmModify.setEnabled(true);
			confirmModify.setBackgroundResource(R.drawable.btn_red_normal);
		}
	}

}
